package com.sysadmin.service;

import java.util.List;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.sysadmin.vo.RolesVO;

public class RoleService extends BaseService {

	
	public List find(RolesVO vo,PageHelper pager){
		StringBuilder sql = new StringBuilder("select * from FXXT_SYS_ROLES u  where 1=1 and u.DEL_FLAG='0' ");
		if(vo!=null){
			if(!StringUtil.isBlank(vo.getRoleName())){
				sql.append(" and u.ROLE_NAME like '%"+vo.getRoleName()+"%'");
			}
			
		}
		List<RolesVO> list = DB4ObjUtil.executePager4Obj(new RolesVO(), sql.toString(),pager);
		
		return list;
	}
	
	public RolesVO get(String id){
		String sql = "select * from FXXT_SYS_ROLES where 1=1 and ID=?";
		List<RolesVO> list = DB4ObjUtil.execute4Obj(new RolesVO(), sql, id);
		if(list.size()>0){
			RolesVO vo = list.get(0);
			
			return vo;
		}
		return null;
	}
	
	public String isApply(String roleName,String id){
		String sql = "select * from FXXT_SYS_ROLES where 1=1 and ROLE_NAME=? and ID<>? and DEL_FLAG='0'";
		List<RolesVO> list = DB4ObjUtil.execute4Obj(new RolesVO(), sql, roleName,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(RolesVO vo){
		String sql = "select * from FXXT_SYS_ROLES where 1=1 and ROLE_NAME=? and ID<>? and DEL_FLAG='0'";
		List<RolesVO> list = DB4ObjUtil.execute4Obj(new RolesVO(), sql, vo.getRoleName(),vo.getId());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public void delete(String ids){
		//String sql = "delete from USERS where id=?";
		String sql = "update FXXT_SYS_ROLES set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			deleteRoleMenu(idArr);
		}
	}
	
	public void delete(String[] ids){
		//String sql = "delete from FXXT_SYS_ROLES where id=?";
		String sql = "update FXXT_SYS_ROLES set DEL_FLAG='1' where id=?";
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
			deleteRoleMenu(ids);
		}
		
		
	}
	public String save(RolesVO vo){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				if(StringUtil.isBlank(vo.getId())){
						String id = StringUtil.getUuid();
						vo.setId(id);
						String sql = "insert into FXXT_SYS_ROLES(ID,ROLE_NAME,DEL_FLAG) values(?,?,?)";
						DBBaseUtil.executeUpdate(sql, id,vo.getRoleName(),"0");
				}else{
					deleteRoleMenu(vo);
					String sql = "update FXXT_SYS_ROLES set ROLE_NAME where id=?";
					DBBaseUtil.executeUpdate(sql,vo.getRoleName(), vo.getId());
				}
				saveRoleMenu(vo);
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public void saveRoleMenu(RolesVO vo){
		
		String sql = "insert into FXXT_SYS_ROLETOMENU(ROLE_ID,MENU_ID) values('"+vo.getId()+"',?)";
		DBBaseUtil.executeBatchUpdate(sql, vo.getMenuIds().split(","));
		
	}
	public void deleteRoleMenu(RolesVO vo){
		
		String sql = "delete from FXXT_SYS_ROLETOMENU where ROLE_ID=?";
		DBBaseUtil.executeUpdate(sql, vo.getId());
		
	}
	
	public void deleteRoleMenu(String[] roleIds){
		
		String sql = "delete from FXXT_SYS_ROLETOMENU where ROLE_ID=?";
		DBBaseUtil.executeBatchUpdate(sql, roleIds);
		
	}
	
	public List<RolesVO> findRole(){
		String sql = "select * from FXXT_SYS_ROLES where 1=1 ";
		List<RolesVO> list = DB4ObjUtil.execute4Obj(new RolesVO(), sql);
		
		return list;
	}
	
}
